-- XmlHttpRequest
-- Created by chengb Aug/6/2015
-- http网络请求

require "json"
local schedulerIdArr = {};
local callbackHasDone = {};

-------------------------
-- 获取远程主机的数据
-- @param url      远程主机url地址
-- @param callback 回调函数
-- @param needOverCheck 需要超时检查
function getRemoteHostData(url, callback, needOverCheck)
    assert(type(url) == "string");

    callbackHasDone[url] = nil;
    schedulerIdArr[url] = nil;

    local xhr = cc.XMLHttpRequest:new();
    xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_STRING;

    -- 打开链接
    xhr:open("GET", url);

    local function onReadyStateChange()
        -- 移除定时器
        local schedulerId = schedulerIdArr[url];
        if schedulerId then
            cc.Director:getInstance():getScheduler():unscheduleScriptEntry(schedulerId);
            schedulerIdArr[url] = nil;
        end

        if callbackHasDone[url] then
            -- 已经调用过了
            callbackHasDone[url] = nil;
            return;
        end

        if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then
            local statusString = "Http Status Code:"..xhr.statusText;
            cclog("url = %s,  statusString = %s", url, statusString);

            -- 获得响应数据
            local response = xhr.response;

            if type(callback) == 'function' then
                callback(response);
            end

        else
            -- 没有获取到响应数据
            print("xhr.readyState is:", xhr.readyState, "xhr.status is: ", xhr.status)
            if type(callback) == 'function' then
                callback();
            end
        end
    end

    -- 超时回调
    local function overTime()
        -- 移除定时器
        local schedulerId = schedulerIdArr[url];
        if schedulerId then
            cc.Director:getInstance():getScheduler():unscheduleScriptEntry(schedulerId);
            schedulerIdArr[url] = nil;
        end
        if type(callback) == 'function' then
            callbackHasDone[url] = true;
            callback();
        end
    end

    -- 注册回调
    xhr:registerScriptHandler(onReadyStateChange);

    if needOverCheck then
        -- 定时器/5s没连上就自动执行后面的操作
        schedulerIdArr[url] = cc.Director:getInstance():getScheduler():scheduleScriptFunc(overTime, 5, false);
    end

    -- 发送请求
    xhr:send();
end

-------------------------
-- POST数据到远程主机
-- @param url      远程主机url地址
-- @param callback 回调函数
function postDataToRemoteHost(url, data, callback)
    assert(type(url) == "string");
    assert(type(data) == "string");

    local xhr = cc.XMLHttpRequest:new();
    xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_STRING;

    -- 打开链接
    xhr:open("POST", url);

    local function onReadyStateChange()
        if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then
            local statusString = "Http Status Code:"..xhr.statusText;
            cclog("url = %s,  statusString = %s", url, statusString);

            -- 获得响应数据
            local response = xhr.response;

            if type(callback) == 'function' then
                callback(response);
            end

        else
            -- 没有获取到响应数据
            print("xhr.readyState is:", xhr.readyState, "xhr.status is: ", xhr.status)
            if type(callback) == 'function' then
                callback();
            end
        end
    end

    -- 注册回调
    xhr:registerScriptHandler(onReadyStateChange);

    -- 发送请求
    xhr:send(data);
end


